package app.netant.finishbook.qishu;

import java.io.File;
import java.net.URLDecoder;
import java.net.URLEncoder;

import com.gargoylesoftware.htmlunit.javascript.host.URL;

import app.client.file.downs.FileDown;

public class QishuDown extends Thread {
	
	private String isDown = "0";
	
	private String filePath;
	private String url;
	
	private FileDown down;
	
	public void setFilePath(String filePath) {
		this.filePath = filePath;
	}

	public void setUrl(String url) {
		this.url = url;
	}
	public String isDown() {
		return isDown;
	}

	public void setDown(String isDown) {
		this.isDown = isDown;
	}

	@Override
	public void run() {
		
		try {
			
			File file = new File(this.filePath);
			if(file.exists()){
				file.delete();
			}
			
			String urlName = url.substring(url.lastIndexOf("/")+1);
			urlName = urlName.replaceAll("・", "·");
			urlName = URLEncoder.encode(urlName, "utf-8");
			String urlPlus = url.substring(0,url.lastIndexOf("/"));
			
			System.out.println("下载文件路径："+filePath);
			System.out.println("下载链接地址："+ urlPlus + "/"+ urlName);
			
			this.downFile(filePath, urlPlus + "/"+ urlName);
			this.isDown = "1";
		} catch (Exception e) {
			
			this.isDown = "2";
			
			File file = new File(this.filePath);
			if(file.exists()){
				file.delete();
			}
			
			e.printStackTrace();
			
		}
		
	}
	
	public void downFile(String filePath,String url) throws Exception{		
		this.down = new FileDown();
		down.downImage(url, filePath);		
	}
	
	/**
	 * 强制关闭
	 */
	public void stopThread(){
		if(this.down != null){
			this.down.stop();
		}
	}
	
	public static void main(String[] args) throws Exception {
		QishuDown downControl = new QishuDown();
	
		System.out.println(URLDecoder.decode("%E7%A5%9E%E8%BF%B9%C2%B7%E4%B8%89%E7%95%8C","utf-8"));
		//downControl.downFile("D:/修仙归来在校园.txt", "http://xiazai.xqishu.com/txt/%E4%BF%AE%E4%BB%99%E5%BD%92%E6%9D%A5%E5%9C%A8%E6%A0%A1%E5%9B%AD.txt");
	}
	
}
